/**
 * $Revision$
 * $Date$
 *
 * This file is part of the Solveur repository software.
 * Copyright (C) 2012 Solveur developer team
 * See http://code.google.com/p/solveur/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/

package gougeon.android.solveur;

//import java.text.Normalizer;

//import android.util.Log;

public class MotScore implements Comparable<MotScore> {

	private String mot;
	private int score;

//	private String oldDecoration;
	private String decoration;
	private int fullWord=0;
	private int charMapping[];

	private String startDecorationBoard="<span>",endDecorationBoard="</span>";
	private String startDecorationBlank="<span><span>",endDecorationBlank="</span></span>";

	public MotScore(String mot, int score, int charMapping[])
	{
		int myIdx;
		this.mot=mot;
		this.score=score;
		this.charMapping=charMapping;
		for (myIdx=0; myIdx<mot.length(); myIdx++)
		{
			this.fullWord|=charMapping[myIdx];
		}
	}

	public String getMot()
	{
		return this.mot;
	}

	public int getScore()
	{
		return this.score;
	}
	
	public int getFullWord()
	{
		return this.fullWord;
	}

	public void setDecoration()
	{
		int myIdx;
		String decoratedMot="";
		String wikiStyle = mot.substring(0,1)+mot.toLowerCase().substring(1,mot.length());

		this.decoration="";

		for (myIdx=0; myIdx<mot.length() ;myIdx++)
		{
			if (charMapping[myIdx]==1)
			{
				decoratedMot+=(startDecorationBoard+mot.charAt(myIdx)+endDecorationBoard);
			}
			else if (charMapping[myIdx]==2)
			{
				decoratedMot+=(startDecorationBlank+mot.charAt(myIdx)+endDecorationBlank);
			}
			else
			{
				decoratedMot+=mot.charAt(myIdx);
			}
		}

		this.decoration="<a href=\"http://fr.wikipedia.org/wiki/"+
						wikiStyle+
						"\"  class=\"example\">"+
						decoratedMot+
						"</a> ("+
						this.score+
						") - ";
	}

	/*public void setOldDecoration(String initial_input)
	{
		int jdx,allez=0;
		String remaining_input = initial_input;
		String decorated_mot="";
		String curr_mot_decorated_and_linked="";
		String lower_case=mot.toLowerCase();
		String nice_mot = mot.substring(0,1)+lower_case.substring(1,lower_case.length());

		for (jdx=0; jdx<mot.length() ;jdx++)
		{
			// Enlever les accents pour matcher a l'entrée utilisateur... android 2.3.3 needed ... think about doing another way
			String curr_char=""+mot.charAt(jdx);
			String normalized = Normalizer.normalize(curr_char, Normalizer.Form.NFD);
			char normalized_char=normalized.replaceAll("[^\\p{ASCII}]", "").charAt(0);

			//int whatchar=blob.indexOf(curr_mot.charAt(jdx));
			int norm_char_pos_in_remaining_input=remaining_input.indexOf(normalized_char);
			String blob2="";
			//Log.d("solveur DDD","HOHOHO \""+ remaining_input+"\"/\""+mot+"\" "+jdx + " "+ norm_char_pos_in_remaining_input);

			if (norm_char_pos_in_remaining_input==-1)
			{
				// jdx char not in the remaining input, check if there was a blank tile
				int blank_tile_pos_in_remaining_input=remaining_input.indexOf(' ');
				//Log.d("solveur CCC","HOHOHO \""+ remaining_input+"\"/\""+mot+"\" "+jdx+" "+blank_tile_pos_in_remaining_input);
				if (blank_tile_pos_in_remaining_input==-1)
				{
					// this char is likely to be board one...
					// don't recompute word score ...
					decorated_mot+=("<span>"+curr_char+"</span>");
					//Log.d("solveur EEE","HOHOHO \""+ remaining_input+"\"/\""+mot+"\" "+jdx+" "+blank_tile_pos_in_remaining_input);
					allez|=1;
				}
				else
				{
					blob2=remaining_input.substring(0,blank_tile_pos_in_remaining_input)+remaining_input.substring(blank_tile_pos_in_remaining_input+1,remaining_input.length());
					remaining_input = blob2;       	        	    		
					// recompute word score ...
					//Chaine boardChar=new Chaine(""+normalized_char);
					//this.score-=boardChar.getScore();
					decorated_mot+=("<span><span>"+curr_char+"</span></span>");
					//Log.d("solveur AAA","HOHOHO \""+ remaining_input+"\"/\""+mot+"\" "+jdx+" "+norm_char_pos_in_remaining_input);
					allez|=2;
				}
			}
			else
			{
				// remove this char from blob and go on
				blob2=remaining_input.substring(0,norm_char_pos_in_remaining_input)+remaining_input.substring(norm_char_pos_in_remaining_input+1,remaining_input.length());
				remaining_input = blob2;
				decorated_mot+=curr_char;

				allez|=4;
				//Log.d("solveur BBB","HOHOHO \""+ remaining_input+"\"/\""+mot+"\" "+jdx+" "+norm_char_pos_in_remaining_input);
			}
		}
		curr_mot_decorated_and_linked=	"<a href=\"http://fr.wikipedia.org/wiki/"+
										nice_mot+
										"\"  class=\"example\">"+
										decorated_mot+
										"</a> ("+
										this.score+
										") - ";

		if ((allez&3)==0)
		{
			this.fullWord=2;
		}
		else if ((allez&2)==2)
		{
			this.fullWord=1;
		}

		this.oldDecoration=curr_mot_decorated_and_linked;
	}*/

	/*public String getOldDecoration()
	{
		return this.oldDecoration;
	}*/

	public String getDecoration()
	{
		return this.decoration;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o) {
		if (	(o!=null) && 
				(o instanceof MotScore) &&
				(((MotScore)o).getMot() != null) &&
				(((MotScore)o).getMot().equals(this.getMot()))
			)
		{
			return true;
		}
		return false;
	}

	public int compareTo(MotScore arg0) {
		final int EQUAL = 0;
		final int BEFORE = -1;
		final int AFTER = 1;
		
		if (this == arg0) return EQUAL;
		
		if (this.mot.length() > arg0.getMot().length()) return BEFORE;
		if (this.mot.length() < arg0.getMot().length()) return AFTER;

		if (this.fullWord < arg0.getFullWord()) return BEFORE;
		if (this.fullWord > arg0.getFullWord()) return AFTER;

		if (this.score > arg0.getScore()) return BEFORE;
		if (this.score < arg0.getScore()) return AFTER;

		return EQUAL;
	}
}
